<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.0.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.0.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.0.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.0.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="简介Apollo（阿波罗）是携程框架部门研发的分布式配置中心，能够集中化管理应用不同环境、不同集群的配置，配置修改后能够实时推送到应用端，并且具备规范的权限、流程治理等特性，适用于微服务配置管理场景。 Apollo和Spring Cloud Config对比 通过对比，可以看出，生成环境中 Apollo 相比 Spring Cloud Config 更具有优势一些。 安装 Apollo 配置中心搭">
<meta name="keywords" content="SpringBoot,Apollo">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot 整合 apollo">
<meta property="og:url" content="http://yoursite.com/2019/03/08/SpringBoot-整合-apollo/index.html">
<meta property="og:site_name" content="程序员果果的博客">
<meta property="og:description" content="简介Apollo（阿波罗）是携程框架部门研发的分布式配置中心，能够集中化管理应用不同环境、不同集群的配置，配置修改后能够实时推送到应用端，并且具备规范的权限、流程治理等特性，适用于微服务配置管理场景。 Apollo和Spring Cloud Config对比 通过对比，可以看出，生成环境中 Apollo 相比 Spring Cloud Config 更具有优势一些。 安装 Apollo 配置中心搭">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a6854f0010a?w=797&h=282&f=png&s=61698">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a6ea6c26747?w=2578&h=1426&f=png&s=1071236">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a725eac5f87?w=2578&h=1426&f=png&s=277652">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a76df5e6f57?w=2578&h=1426&f=png&s=200081">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a79ba3a50ae?w=2578&h=1426&f=png&s=215959">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a7e13a9e8fd?w=2578&h=1426&f=png&s=297183">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a81ca72987a?w=2578&h=1426&f=png&s=300679">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a84fc19c2ca?w=2578&h=1426&f=png&s=338606">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a87af6839ec?w=2578&h=1426&f=png&s=296451">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a8b89f29430?w=2578&h=1426&f=png&s=308051">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a8d8135dfc8?w=2104&h=890&f=png&s=125338">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a9001837c80?w=2112&h=766&f=png&s=120007">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a937dc93dd6?w=2578&h=1426&f=png&s=301475">
<meta property="og:updated_time" content="2019-03-08T02:33:50.482Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SpringBoot 整合 apollo">
<meta name="twitter:description" content="简介Apollo（阿波罗）是携程框架部门研发的分布式配置中心，能够集中化管理应用不同环境、不同集群的配置，配置修改后能够实时推送到应用端，并且具备规范的权限、流程治理等特性，适用于微服务配置管理场景。 Apollo和Spring Cloud Config对比 通过对比，可以看出，生成环境中 Apollo 相比 Spring Cloud Config 更具有优势一些。 安装 Apollo 配置中心搭">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2019/3/6/16950a6854f0010a?w=797&h=282&f=png&s=61698">






  <link rel="canonical" href="http://yoursite.com/2019/03/08/SpringBoot-整合-apollo/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>SpringBoot 整合 apollo | 程序员果果的博客</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?48ba4ef106e9b2430083b8ec78a1831e";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">程序员果果的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/08/SpringBoot-整合-apollo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="程序员果果">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="程序员果果的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">SpringBoot 整合 apollo

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-03-08 00:10:38 / 修改时间：10:33:50" itemprop="dateCreated datePublished" datetime="2019-03-08T00:10:38+08:00">2019-03-08</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/SpringBoot/" itemprop="url" rel="index"><span itemprop="name">SpringBoot</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>Apollo（阿波罗）是携程框架部门研发的分布式配置中心，能够集中化管理应用不同环境、不同集群的配置，配置修改后能够实时推送到应用端，并且具备规范的权限、流程治理等特性，适用于微服务配置管理场景。</p>
<h2 id="Apollo和Spring-Cloud-Config对比"><a href="#Apollo和Spring-Cloud-Config对比" class="headerlink" title="Apollo和Spring Cloud Config对比"></a>Apollo和Spring Cloud Config对比</h2><p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a6854f0010a?w=797&amp;h=282&amp;f=png&amp;s=61698" alt></p>
<p>通过对比，可以看出，生成环境中 Apollo 相比 Spring Cloud Config 更具有优势一些。</p>
<h2 id="安装-Apollo-配置中心"><a href="#安装-Apollo-配置中心" class="headerlink" title="安装 Apollo 配置中心"></a>安装 Apollo 配置中心</h2><h3 id="搭建教程"><a href="#搭建教程" class="headerlink" title="搭建教程"></a>搭建教程</h3><p>参照 <a href="https://github.com/ctripcorp/apollo/wiki/Quick-Start" target="_blank" rel="noopener">https://github.com/ctripcorp/apollo/wiki/Quick-Start</a> 搭建 Apollo 配置中心，文档写的很清楚，这里就赘述了。</p>
<h3 id="查看样例配置"><a href="#查看样例配置" class="headerlink" title="查看样例配置"></a>查看样例配置</h3><p>搭建完成并启动后，访问 <a href="http://localhost:8070" target="_blank" rel="noopener">http://localhost:8070</a> ，界面如下。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a6ea6c26747?w=2578&amp;h=1426&amp;f=png&amp;s=1071236" alt></p>
<p>输入用户名 apollo，密码 admin 后登录后，点击SampleApp进入配置界面。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a725eac5f87?w=2578&amp;h=1426&amp;f=png&amp;s=277652" alt></p>
<h2 id="与-Spring-Boot-整合使用"><a href="#与-Spring-Boot-整合使用" class="headerlink" title="与 Spring Boot 整合使用"></a>与 Spring Boot 整合使用</h2><p>创建一个springboot项目，主要代码如下。</p>
<h3 id="pom-xml"><a href="#pom-xml" class="headerlink" title="pom.xml"></a>pom.xml</h3><p>添加 Apollo 客户端的依赖，为了编码方便引入commons-lang3。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ctrip.framework.apollo<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>apollo-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 为了编码方便，并非apollo 必须的依赖 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.commons<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-lang3<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.8.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="application-yml"><a href="#application-yml" class="headerlink" title="application.yml"></a>application.yml</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">server:</span><br><span class="line">  port: <span class="number">8761</span></span><br><span class="line"></span><br><span class="line">app:</span><br><span class="line">  id: springboot-apollo</span><br><span class="line">apollo:</span><br><span class="line">  meta: http:<span class="comment">//127.0.0.1:8080</span></span><br><span class="line">  bootstrap:</span><br><span class="line">    enabled: <span class="literal">true</span></span><br><span class="line">    eagerLoad:</span><br><span class="line">      enabled: <span class="literal">true</span></span><br><span class="line"></span><br><span class="line">logging:</span><br><span class="line">  level:</span><br><span class="line">    com:</span><br><span class="line">      gf:</span><br><span class="line">        controller: debug</span><br></pre></td></tr></table></figure>
<p>配置说明：</p>
<ul>
<li><strong>app.id</strong>：AppId是应用的身份信息，是配置中心获取配置的一个重要信息。</li>
<li><strong>apollo.bootstrap.enabled</strong>：在应用启动阶段，向Spring容器注入被托管的application.properties文件的配置信息。</li>
<li><strong>apollo.bootstrap.eagerLoad.enabled</strong>：将Apollo配置加载提到初始化日志系统之前。</li>
<li><strong>logging.level.com.gf.controller</strong>：调整 controller 包的 log 级别，为了后面演示在配置中心动态配置日志级别。</li>
</ul>
<h3 id="HelloController"><a href="#HelloController" class="headerlink" title="HelloController"></a>HelloController</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Logger logger = LoggerFactory.getLogger( HelloController.class );</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value</span>( <span class="string">"$&#123;server.port&#125;"</span> )</span><br><span class="line">    String port;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping</span>(<span class="string">"hi"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">hi</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        logger.debug( <span class="string">"debug log..."</span> );</span><br><span class="line">        logger.info( <span class="string">"info log..."</span> );</span><br><span class="line">        logger.warn( <span class="string">"warn log..."</span> );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"hi "</span> + name + <span class="string">" ,i am from port:"</span> + port;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="启动类"><a href="#启动类" class="headerlink" title="启动类"></a>启动类</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableApolloConfig</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpringbootApolloApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run( SpringbootApolloApplication.class, args );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>启动项目。现在需要去配置中心做些关于这个springboot客户端的一些配置。</p>
<h3 id="配置中心的配置"><a href="#配置中心的配置" class="headerlink" title="配置中心的配置"></a>配置中心的配置</h3><h4 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h4><p>第一步：访问<a href="http://localhost:8070" target="_blank" rel="noopener">http://localhost:8070</a> 登录后，选择创建项目。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a76df5e6f57?w=2578&amp;h=1426&amp;f=png&amp;s=200081" alt></p>
<p>第二步：填写配置信息。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a79ba3a50ae?w=2578&amp;h=1426&amp;f=png&amp;s=215959" alt></p>
<p>配置说明：</p>
<ul>
<li><strong>部门</strong>：选择应用所在的部门。（想自定义部门，参照官方文档，这里就选择样例）</li>
<li><strong>应用AppId</strong>：用来标识应用身份的唯一id，格式为string，需要和客户端。application.properties中配置的app.id对应。</li>
<li><strong>应用名称</strong>：应用名，仅用于界面展示。</li>
<li><strong>应用负责人</strong>：选择的人默认会成为该项目的管理员，具备项目权限管理、集群创建、Namespace创建等权限。</li>
</ul>
<p>提交配置后会出现如下项目配置的管理页面。</p>
<h4 id="添加配置项"><a href="#添加配置项" class="headerlink" title="添加配置项"></a>添加配置项</h4><p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a7e13a9e8fd?w=2578&amp;h=1426&amp;f=png&amp;s=297183" alt></p>
<p>第一步：点击 “新增配置”，配置需要管理的 application.properties 中的属性。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a81ca72987a?w=2578&amp;h=1426&amp;f=png&amp;s=300679" alt></p>
<p>点击提交，之后按照同样的方法，新增需要动态管理的 application.properties 中的属性。</p>
<p>提交后，跳转到配置的管理界面：</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a84fc19c2ca?w=2578&amp;h=1426&amp;f=png&amp;s=338606" alt></p>
<h4 id="发布配置"><a href="#发布配置" class="headerlink" title="发布配置"></a>发布配置</h4><p>配置只有在发布后才会真的被应用使用到，所以在编辑完配置后，需要发布配置。点击“发布按钮”。</p>
<p>填写发布相关信息，点击发布 。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a87af6839ec?w=2578&amp;h=1426&amp;f=png&amp;s=296451" alt></p>
<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p>在配置中心，修改 server.port 的值为 8762 并发布。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a8b89f29430?w=2578&amp;h=1426&amp;f=png&amp;s=308051" alt></p>
<p>Postman 访问之前写个测试接口 <a href="http://127.0.0.1:8761/hi?name=zhangsan" target="_blank" rel="noopener">http://127.0.0.1:8761/hi?name=zhangsan</a> ，返回如下。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a8d8135dfc8?w=2104&amp;h=890&amp;f=png&amp;s=125338" alt></p>
<p>说明 客户端 获取到了 配置中心修改后的 server.port 的值 。</p>
<p>注意：</p>
<ul>
<li>服务的端口依然还是 8761，这是因为 apollo 修改配置，不会像Spring Cloud Config 那样去重启应用。</li>
<li>重启应用后，客户端会加载使用 配置中心中配置的属性的值，例如我们重启我们的应用，服务端口会变成8762。</li>
</ul>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a9001837c80?w=2112&amp;h=766&amp;f=png&amp;s=120007" alt></p>
<h3 id="监听配置的变化"><a href="#监听配置的变化" class="headerlink" title="监听配置的变化"></a>监听配置的变化</h3><h4 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h4><p>日志模块是每个项目中必须的，用来记录程序运行中的相关信息。一般在开发环境下使用DEBUG级别的日志输出，为了方便查看问题，而在线上一般都使用INFO或者ERROR级别的日志，主要记录业务操作或者错误的日志。那么问题来了，当线上环境出现问题希望输出DEBUG日志信息辅助排查的时候怎么办呢？修改配置文件，重新打包然后上传重启线上环境，以前确实是这么做的。</p>
<p>虽然上面我们已经把日志的配置部署到Apollo配置中心，但在配置中心修改日志等级，依然需要重启应用才生效，下面我们就通过监听配置的变化，来达到热更新的效果。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LoggerConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger logger = LoggerFactory.getLogger(LoggerConfig.class);</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String LOGGER_TAG = <span class="string">"logging.level."</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> LoggingSystem loggingSystem;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApolloConfig</span></span><br><span class="line">    <span class="keyword">private</span> Config config;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ApolloConfigChangeListener</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">configChangeListter</span><span class="params">(ConfigChangeEvent changeEvent)</span> </span>&#123;</span><br><span class="line">        refreshLoggingLevels();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostConstruct</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">refreshLoggingLevels</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Set&lt;String&gt; keyNames = config.getPropertyNames();</span><br><span class="line">        <span class="keyword">for</span> (String key : keyNames) &#123;</span><br><span class="line">            <span class="keyword">if</span> (StringUtils.containsIgnoreCase(key, LOGGER_TAG)) &#123;</span><br><span class="line">                String strLevel = config.getProperty(key, <span class="string">"info"</span>);</span><br><span class="line">                LogLevel level = LogLevel.valueOf(strLevel.toUpperCase());</span><br><span class="line">                loggingSystem.setLogLevel(key.replace(LOGGER_TAG, <span class="string">""</span>), level);</span><br><span class="line">                logger.info(<span class="string">"&#123;&#125;:&#123;&#125;"</span>, key, strLevel);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>关键点讲解：</p>
<ul>
<li><strong>@ApolloConfig注解</strong>：将Apollo服务端的中的配置注入这个类中。</li>
<li><strong>@ApolloConfigChangeListener注解</strong>：监听配置中心配置的更新事件，若该事件发生，则调用refreshLoggingLevels方法，处理该事件。</li>
<li><strong>ConfigChangeEvent参数</strong>：可以获取被修改配置项的key集合，以及被修改配置项的新值、旧值和修改类型等信息。</li>
</ul>
<p>application.yml 中配置的日志级别是 debug ，访问<a href="http://127.0.0.1:8761/hi?name=zhangsan" target="_blank" rel="noopener">http://127.0.0.1:8761/hi?name=zhangsan</a> ，控制台打印如下。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">2019-03-05 18:29:22.673 DEBUG 4264 --- [nio-8762-exec-1] com.gf.controller.HelloController        : debug log...</span><br><span class="line">2019-03-05 18:29:22.673  INFO 4264 --- [nio-8762-exec-1] com.gf.controller.HelloController        : info log...</span><br><span class="line">2019-03-05 18:29:22.673  WARN 4264 --- [nio-8762-exec-1] com.gf.controller.HelloController        : warn log...</span><br></pre></td></tr></table></figure>
<p>现在在配置中心修改日志级别为 warn。</p>
<p><img src="https://user-gold-cdn.xitu.io/2019/3/6/16950a937dc93dd6?w=2578&amp;h=1426&amp;f=png&amp;s=301475" alt></p>
<p>再次访问<a href="http://127.0.0.1:8761/hi?name=zhangsan" target="_blank" rel="noopener">http://127.0.0.1:8761/hi?name=zhangsan</a> ，控制台打印如下。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2019-03-05 19:07:19.469  WARN 4264 --- [nio-8762-exec-3] com.gf.controller.HelloController        : warn log...</span><br></pre></td></tr></table></figure>
<p>说明日志级别的配置，已经支持热更新了。关于apollo 的更多应用 ，可以参照github的文档。</p>
<p>源码下载 : <a href="https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-apollo" target="_blank" rel="noopener">https://github.com/gf-huanchupk/SpringBootLearning/tree/master/springboot-apollo</a></p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/SpringBoot/" rel="tag"># SpringBoot</a>
          
            <a href="/tags/Apollo/" rel="tag"># Apollo</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/03/08/SpringBoot-整合-elk/" rel="prev" title="SpringBoot 整合 elk">
                SpringBoot 整合 elk <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/avatar.jpg" alt="程序员果果">
            
              <p class="site-author-name" itemprop="name">程序员果果</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">17</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">22</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/gf-huanchupk" title="GitHub &rarr; https://github.com/gf-huanchupk" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:782969359@qq.com" title="E-Mail &rarr; mailto:782969359@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Apollo和Spring-Cloud-Config对比"><span class="nav-number">2.</span> <span class="nav-text">Apollo和Spring Cloud Config对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#安装-Apollo-配置中心"><span class="nav-number">3.</span> <span class="nav-text">安装 Apollo 配置中心</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#搭建教程"><span class="nav-number">3.1.</span> <span class="nav-text">搭建教程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看样例配置"><span class="nav-number">3.2.</span> <span class="nav-text">查看样例配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#与-Spring-Boot-整合使用"><span class="nav-number">4.</span> <span class="nav-text">与 Spring Boot 整合使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#pom-xml"><span class="nav-number">4.1.</span> <span class="nav-text">pom.xml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#application-yml"><span class="nav-number">4.2.</span> <span class="nav-text">application.yml</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HelloController"><span class="nav-number">4.3.</span> <span class="nav-text">HelloController</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#启动类"><span class="nav-number">4.4.</span> <span class="nav-text">启动类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置中心的配置"><span class="nav-number">4.5.</span> <span class="nav-text">配置中心的配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建项目"><span class="nav-number">4.5.1.</span> <span class="nav-text">创建项目</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#添加配置项"><span class="nav-number">4.5.2.</span> <span class="nav-text">添加配置项</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#发布配置"><span class="nav-number">4.5.3.</span> <span class="nav-text">发布配置</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#测试"><span class="nav-number">4.6.</span> <span class="nav-text">测试</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监听配置的变化"><span class="nav-number">4.7.</span> <span class="nav-text">监听配置的变化</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#需求"><span class="nav-number">4.7.1.</span> <span class="nav-text">需求</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">程序员果果</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.0.1</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.1"></script>

  <script src="/js/src/motion.js?v=7.0.1"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.1"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.1"></script>




  
  <script src="/js/src/scrollspy.js?v=7.0.1"></script>
<script src="/js/src/post-details.js?v=7.0.1"></script>



  


  <script src="/js/src/next-boot.js?v=7.0.1"></script>


  

  


  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
